package com.guyu.plugin.password.crypto.impl;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.digest.MD5;
import com.guyu.plugin.password.crypto.AbstractCrypto;

import java.nio.charset.StandardCharsets;

public class RsaCrypto extends AbstractCrypto {
    private static final long serialVersionUID = -5071111080196757203L;
    private RSA rsa;
    private MD5 md5;

    public RsaCrypto(String publicKeyStr, String privateKeyStr, String salt) {
        super(publicKeyStr, privateKeyStr, salt);
        this.rsa = SecureUtil.rsa(privateKeyStr, publicKeyStr);
        this.md5 = new MD5(salt.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String encryptHex(String data) {
        return rsa.encryptHex(data, KeyType.PublicKey);
    }

    @Override
    public String decryptStr(String data) {
        return rsa.decryptStr(data, KeyType.PrivateKey);
    }

    @Override
    public String digestHex(String data) {
        return md5.digestHex(data);
    }
}
